// import axios from 'axios'
const axios = require('axios')
const cancelerMap = new Map()

 class AxiosCancel {
    static instance
    static createInstance() {
        return this.instance ?? (this.instance = new AxiosCancel())
    }
    add(config) {
        const url = config.url
        this.remove(url)
        config.cancelToken = new axios.CancelToken((cancel) => {
            if (!cancelerMap.has(url)) {
                cancelerMap.set(url, cancel)
            }
        })
    }
    remove(url) {
        if (cancelerMap.has(url)) {
            const cancel = cancelerMap.get(url)
            cancel && cancel(url)
            cancelerMap.delete(url)
        }
    }
}

const axiosCancel = AxiosCancel.createInstance()

exports.axiosCancel = axiosCancel